﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace IndexedList
{
    class FieldInfoResolver<T> : ExpressionVisitor
    {

        public static Dictionary<string, PropertyInfo> GetFieldNames<TData>(Expression expression)
        {
            FieldInfoResolver<TData> indexvisitor = new FieldInfoResolver<TData>();
            indexvisitor.Visit(expression);
            return indexvisitor.Fields;
        }

        public Dictionary<string, PropertyInfo> Fields = new Dictionary<string, PropertyInfo>();

        public override Expression Visit(Expression node)
        {
            return base.Visit(node);
        }

        protected override Expression VisitMember(MemberExpression node)
        {
            if (node.Expression.NodeType == ExpressionType.Parameter && node.Member.DeclaringType.Equals(typeof(T)))
            {
                if (node.Member.MemberType == MemberTypes.Property)
                    Fields.Add(node.Member.Name, (PropertyInfo)node.Member);
            }
            return base.VisitMember(node);
        }
    }


}
